package csel.view;

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import csel.model.entity.Occupation;
import csel.model.entity.Smasher;
import csel.model.entity.Sneak;
import csel.model.entity.Summoner;

public class NewGameDialog extends Viewport
{
	private static final String setOccupationCmd = "setOccupation";
	
	/* UI STUFF */
	private ButtonGroup buttonGroup;
	private JTextField txtName;
	private JRadioButton summonerButton, smasherButton, sneakButton;
	private JButton btnOK, btnCancel;
	private JDialog dialog;
	
	/* UI cached values */
	private volatile Occupation cOccupation;
	
	/**
	 * Remembers whether the user OK'd or canceled.
	 */
	private boolean ret;
	
	public NewGameDialog()
	{
		JPanel contentPane;
		JPanel tempPanel;
		MyActionListener listener = new MyActionListener();
		
		dialog = new JDialog((java.awt.Frame)null, true);
		
		contentPane = new JPanel(new GridLayout(0,1));	// I don't know why we need this but...
		
		/* Name panel */
		
		tempPanel = new JPanel();
		
		tempPanel.add(new JLabel("Name:"));
		txtName = new JTextField("No-Name", 10);
		tempPanel.add(txtName);
		
		contentPane.add(tempPanel);
		
		/* Occupation Buttons */

		tempPanel = new JPanel();
		smasherButton = new JRadioButton("Smasher");
		smasherButton.setSelected(true);
		smasherButton.setActionCommand(setOccupationCmd);
		smasherButton.addActionListener(listener);
		smasherButton.putClientProperty("occupation", cOccupation = new Smasher());	// note this
		summonerButton = new JRadioButton("Summoner");
		summonerButton.setActionCommand(setOccupationCmd);
		summonerButton.addActionListener(listener);
		summonerButton.putClientProperty("occupation", new Summoner());
		sneakButton = new JRadioButton("Sneak");
		sneakButton.setActionCommand(setOccupationCmd);
		sneakButton.addActionListener(listener);
		sneakButton.putClientProperty("occupation", new Sneak());

		buttonGroup = new ButtonGroup();
		buttonGroup.add(smasherButton);
		buttonGroup.add(summonerButton);
		buttonGroup.add(sneakButton);

		tempPanel.add(smasherButton);
		tempPanel.add(summonerButton);
		tempPanel.add(sneakButton);
		contentPane.add(tempPanel);
		
		/* Buttons */
		
		tempPanel = new JPanel();
		
		btnOK = new JButton("OK");
		btnOK.addActionListener(listener);
		tempPanel.add(btnOK);
		
		btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(listener);
		tempPanel.add(btnCancel);
		
		contentPane.add(tempPanel);

		dialog.setContentPane(contentPane);

		dialog.getRootPane().setDefaultButton(btnOK);
		dialog.setTitle("Customize your character.");
		
		dialog.pack();
		
		ret = false;	// assume cancel unless ok
	}
	
	@Override
	public void close()
	{
		dialog.setVisible(false);
		dialog.dispose();
	}

	@Override
	protected Container getContainer()
	{
		return dialog;
	}

	@Override
	public void setVisible(boolean visibility)
	{
		dialog.setVisible(visibility);
	}
	
	/**
	 * Modally displays dialog to user.
	 * @return true if user clicked "OK"
	 */
	public boolean show()
	{
		dialog.setVisible(true);
		
		return ret;
	}
	
	/**
	 * 
	 * @return the name the user entered
	 */
	public String getName()
	{
		String name = txtName.getText();
		
		return (name != null) ? name : "No-Name";
	}
	
	/**
	 * 
	 * @return the occupation the user selected
	 */
	public csel.model.entity.Occupation getOccupation()
	{
		return cOccupation;
	}
	
	private class MyActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent ev)
		{
			Object source = ev.getSource(); 
			if (source == btnOK)
			{
				ret = true;
				dialog.dispose();
			}
			else if (source == btnCancel)
			{
				dialog.dispose();
			}
			else if (setOccupationCmd.equals(ev.getActionCommand()))
			{
				cOccupation =
					(Occupation)((JRadioButton)source).getClientProperty("occupation");
			}
		}
	}
}
